import {createI18n} from 'vue-i18n';
import elEnLocale from 'element-plus/es/locale/lang/en.mjs';
import elZhLocale  from 'element-plus/es/locale/lang/zh-cn.mjs';
import enLocale  from './en.js';
import zhLocale  from './zh-cn.js';
import {CacheUtil} from "@/utils/cacheUtil.js";

const messages = {
    en: {
        ...enLocale,
        ...elEnLocale
    },
    'zh-cn': {
        ...zhLocale,
        ...elZhLocale
    }
}

export const getLocale = () => {
    //获取缓存
    const storeLanguage = CacheUtil.getLanguage();
    //存在返回当前语言
    if (storeLanguage) return storeLanguage;
    //不存在获取系统语言
    const language = (navigator.language || navigator.browserLanguage).toLowerCase();
    const locales = Object.keys(messages);
    for (const locale of locales) {
        if (language.indexOf(locale) > -1) {
            return locale;
        }
    }
    //返回默认简体中文
    return 'zh-cn';
}

const i18n = createI18n({
    globalInjection: true,
    legacy: false,
    locale: getLocale(),
    messages: messages,
})

export default i18n;